package com.xingyi.bacs.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.xingyi.bacs.bean.*;
import com.xingyi.bacs.dao.FrDao;
import com.xingyi.bacs.mapper.FrMapper;
import com.xingyi.bacs.util.DesUtil;
import com.xingyi.bacs.util.LogUtil;
import com.xingyi.bacs.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wujt on 30/9/2021
 */
@Service
@DS("fr")
@Transactional
public class FrServiceImpl implements FrService{

    private int save_count = 1000;

    @Resource
    private FrMapper frMapper;
    @Resource
    private FrDao frDao;

    @Override
    public List<RJFRAccountInfo> runRJFRAccountInfo(String date) {
        List<RJFRAccountInfo> list = null;
        List<Long> idList = new ArrayList<Long>();
        List<RJFRAccountInfo> saveList = new ArrayList<RJFRAccountInfo>();
        try {
            list = frMapper.queryRJFRAccountInfo(date);
            if (list!=null&&list.size()>0) {
                for (RJFRAccountInfo data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    idList.add(data.getId());
                    saveList.add(data);
                }
            }
            //根据id删除RUN_表中的数据（分批删除，每1000条）
            if (idList!=null && idList.size()>0){
                int num = idList.size() / save_count;
                if (num>0){
                    for (int i=0; i<num; i++){
                        LogUtil.info(FrServiceImpl.class,"【RUN_RJFR_ACCOUNTINFO】开始删除第"+(i*save_count)+"~"+((i+1)*save_count)+"条数据");
                        frMapper.deleteRJFRAccountInfoById(idList.subList(i*save_count,(i+1)*save_count));
                    }
                    LogUtil.info(FrServiceImpl.class,"【RUN_RJFR_ACCOUNTINFO】开始删除第"+(num*save_count)+"~"+(idList.size())+"条数据");
                    frMapper.deleteRJFRAccountInfoById(idList.subList(num*save_count,idList.size()));
                }else{
                    LogUtil.info(FrServiceImpl.class,"【RUN_RJFR_ACCOUNTINFO】开始删除第"+1+"~"+(idList.size())+"条数据");
                    frMapper.deleteRJFRAccountInfoById(idList);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRJFRAccountInfo(saveList);
                LogUtil.info(FrServiceImpl.class,"本次账户结算信息表【RJFR_ACCOUNTINFO】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"本次账户结算信息表【RJFR_ACCOUNTINFO】无数据进行跑批");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<DLRAccountInfo> runDLRAccountInfo(String date) {
        List<DLRAccountInfo> list = null;
        List<Long> idList = new ArrayList<Long>();
        List<DLRAccountInfo> saveList = new ArrayList<DLRAccountInfo>();
        try {
            list = frMapper.queryDLRAccountInfo(date);
            if (list!=null&&list.size()>0) {
                for (DLRAccountInfo data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    idList.add(data.getId());
                    saveList.add(data);
                }
            }
            //根据id删除RUN_表中的数据（分批删除，每1000条）
            if (idList!=null && idList.size()>0){
                int num = idList.size() / save_count;
                if (num>0){
                    for (int i=0; i<num; i++){
                        LogUtil.info(FrServiceImpl.class,"【RUN_DLR_ACCOUNTINFO】开始删除第"+(i*save_count)+"~"+((i+1)*save_count)+"条数据");
                        frMapper.deleteDLRAccountInfoById(idList.subList(i*save_count,(i+1)*save_count));
                    }
                    LogUtil.info(FrServiceImpl.class,"【RUN_DLR_ACCOUNTINFO】开始删除第"+(num*save_count)+"~"+(idList.size())+"条数据");
                    frMapper.deleteDLRAccountInfoById(idList.subList(num*save_count,idList.size()));
                }else{
                    LogUtil.info(FrServiceImpl.class,"【RUN_DLR_ACCOUNTINFO】开始删除第"+1+"~"+(idList.size())+"条数据");
                    frMapper.deleteDLRAccountInfoById(idList);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertDLRAccountInfo(saveList);
                LogUtil.info(FrServiceImpl.class,"本次代理人账户结算信息表【DLR_ACCOUNTINFO】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"本次代理人账户结算信息表【DLR_ACCOUNTINFO】无数据进行跑批");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<RJAccountSum> runRJAccountSum(String date) {
        List<RJAccountSum> list = null;
        List<Long> idList = new ArrayList<Long>();
        List<RJAccountSum> saveList = new ArrayList<RJAccountSum>();
        try {
            list = frMapper.queryRJAccountSum(date);
            if (list!=null&&list.size()>0) {
                for (RJAccountSum data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    idList.add(data.getId());
                    saveList.add(data);
                }
            }
            //根据id删除RUN_表中的数据（分批删除，每1000条）
            if (idList!=null && idList.size()>0){
                int num = idList.size() / save_count;
                if (num>0){
                    for (int i=0; i<num; i++){
                        LogUtil.info(FrServiceImpl.class,"【RUN_RJFR_ACCOUNTSUM】开始删除第"+(i*save_count)+"~"+((i+1)*save_count)+"条数据");
                        frMapper.deleteRJAccountSumById(idList.subList(i*save_count,(i+1)*save_count));
                    }
                    LogUtil.info(FrServiceImpl.class,"【RUN_RJFR_ACCOUNTSUM】开始删除第"+(num*save_count)+"~"+(idList.size())+"条数据");
                    frMapper.deleteRJAccountSumById(idList.subList(num*save_count,idList.size()));
                }else{
                    LogUtil.info(FrServiceImpl.class,"【RUN_RJFR_ACCOUNTSUM】开始删除第"+1+"~"+(idList.size())+"条数据");
                    frMapper.deleteRJAccountSumById(idList);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRJAccountSum(saveList);
                LogUtil.info(FrServiceImpl.class,"结算信息汇总表【RJFR_ACCOUNTSUM】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"结算信息汇总表【RJFR_ACCOUNTSUM】无数据进行跑批");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<DLRAccountSum> runDLRAccountSum(String date) {
        List<DLRAccountSum> list = null;
        List<Long> idList = new ArrayList<Long>();
        List<DLRAccountSum> saveList = new ArrayList<DLRAccountSum>();
        try {
            list = frMapper.queryDLRAccountSum(date);
            if (list!=null&&list.size()>0) {
                for (DLRAccountSum data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    idList.add(data.getId());
                    saveList.add(data);
                }
            }
            //根据id删除RUN_表中的数据（分批删除，每1000条）
            if (idList!=null && idList.size()>0){
                int num = idList.size() / save_count;
                if (num>0){
                    for (int i=0; i<num; i++){
                        LogUtil.info(FrServiceImpl.class,"【RUN_DLR_ACCOUNTSUM】开始删除第"+(i*save_count)+"~"+((i+1)*save_count)+"条数据");
                        frMapper.deleteDLRAccountSumById(idList.subList(i*save_count,(i+1)*save_count));
                    }
                    LogUtil.info(FrServiceImpl.class,"【RUN_DLR_ACCOUNTSUM】开始删除第"+(num*save_count)+"~"+(idList.size())+"条数据");
                    frMapper.deleteDLRAccountSumById(idList.subList(num*save_count,idList.size()));
                }else{
                    LogUtil.info(FrServiceImpl.class,"【RUN_DLR_ACCOUNTSUM】开始删除第"+1+"~"+(idList.size())+"条数据");
                    frMapper.deleteDLRAccountSumById(idList);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertDLRAccountSum(saveList);
                LogUtil.info(FrServiceImpl.class,"代理人账户汇总表【DLR_ACCOUNTSUM】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"代理人账户汇总表【DLR_ACCOUNTSUM】无数据进行跑批");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<RjfrTxWater> runRjfrTxWater(String date) {
        List<RjfrTxWater> list = null;
        List<RjfrTxWater> saveList = new ArrayList<RjfrTxWater>();
        try{
            list = frMapper.queryRjfrTxWater(date);
            if (list!=null&&list.size()>0) {
                for (RjfrTxWater data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRjfrTxWater(saveList);
                LogUtil.info(FrServiceImpl.class,"提现流水表【RJFR_TX_WATER】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"提现流水表【RJFR_TX_WATER】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RJAccountFlow> runRJAccountFlow(String date) {
        List<RJAccountFlow> list = null;
        List<RJAccountFlow> saveList = new ArrayList<RJAccountFlow>();
        try{
            list = frMapper.queryRJAccountFlow(date);
            if (list!=null&&list.size()>0) {
                for (RJAccountFlow data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRJAccountFlow(saveList);
                LogUtil.info(FrServiceImpl.class,"账户流水表【RJFR_ACCOUNT_FLOW】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"账户流水表【RJFR_ACCOUNT_FLOW】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RJCzFrWater> runRJCzFrWater(String date) {
        List<RJCzFrWater> list = null;
        List<RJCzFrWater> saveList = new ArrayList<RJCzFrWater>();
        try{
            list = frMapper.queryRJCzFrWater(date);
            if (list!=null&&list.size()>0) {
                for (RJCzFrWater data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRJCzFrWater(saveList);
                LogUtil.info(FrServiceImpl.class,"充值分润流水表【RJFR_CZFR_WATER】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"充值分润流水表【RJFR_CZFR_WATER】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RJCzFxWater> runRJCzFxWater(String date) {
        List<RJCzFxWater> list = null;
        List<RJCzFxWater> saveList = new ArrayList<RJCzFxWater>();
        try{
            list = frMapper.queryRJCzFxWater(date);
            if (list!=null&&list.size()>0) {
                for (RJCzFxWater data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRJCzFxWater(saveList);
                LogUtil.info(FrServiceImpl.class,"充值返现流水表【RJFR_CZFX_WATER】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"充值返现流水表【RJFR_CZFX_WATER】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RJFyWater> runRJFyWater(String date) {
        List<RJFyWater> list = null;
        List<RJFyWater> saveList = new ArrayList<RJFyWater>();
        try{
            list = frMapper.queryRJFyWater(date);
            if (list!=null&&list.size()>0) {
                for (RJFyWater data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRJFyWater(saveList);
                LogUtil.info(FrServiceImpl.class,"费用流水表【RJFR_FY_WATER】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"费用流水表【RJFR_FY_WATER】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RJGcWater> runRJGcWater(String date) {
        List<RJGcWater> list = null;
        List<RJGcWater> saveList = new ArrayList<RJGcWater>();
        try{
            list = frMapper.queryRJGcWater(date);
            if (list!=null&&list.size()>0) {
                for (RJGcWater data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertRJGcWater(saveList);
                LogUtil.info(FrServiceImpl.class,"轧差流水表【RJFR_GC_WATER】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"轧差流水表【RJFR_GC_WATER】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<MJDetailWater> runMJDetailWater(String date) {
        List<MJDetailWater> list = null;
        List<MJDetailWater> saveList = new ArrayList<MJDetailWater>();
        try{
            list = frMapper.queryMJDetailWater(date);
            if (list!=null&&list.size()>0) {
                for (MJDetailWater data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertMJDetailWater(saveList);
                LogUtil.info(FrServiceImpl.class,"秒结明细流水表【MJ_DETAIL_WATER】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"秒结明细流水表【MJ_DETAIL_WATER】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TbAccountInfo> runTbAccountInfo(String date) {
        List<TbAccountInfo> list = null;
        List<TbAccountInfo> saveList = new ArrayList<TbAccountInfo>();
        try{
            list = frMapper.queryTbAccountInfo(date);
            if (list!=null&&list.size()>0) {
                for (TbAccountInfo data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertTbAccountInfo(saveList);
                LogUtil.info(FrServiceImpl.class,"同步结算信息表【TB_ACCOUNTINFO】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"同步结算信息表【TB_ACCOUNTINFO】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<DLRTbAccountInfo> runDLRTbAccountInfo(String date) {
        List<DLRTbAccountInfo> list = null;
        List<DLRTbAccountInfo> saveList = new ArrayList<DLRTbAccountInfo>();
        try{
            list = frMapper.queryDLRTbAccountInfo(date);
            if (list!=null&&list.size()>0) {
                for (DLRTbAccountInfo data : list) {
                    if (StringUtil.hasValue(data.getSjh())) {
                        data.setSjh(DesUtil.encrypt(data.getSjh(), DesUtil.KEY));
                    }
                    saveList.add(data);
                }
            }
            //将数据插入到RUN_表中
            if (saveList!=null && saveList.size()>0){
                frDao.insertDLRTbAccountInfo(saveList);
                LogUtil.info(FrServiceImpl.class,"代理人同步结算信息表【DLR_TB_ACCOUNTINFO】跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(FrServiceImpl.class,"代理人同步结算信息表【DLR_TB_ACCOUNTINFO】无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteRjfrTxWater(String date) {
        try {
            frMapper.deleteRjfrTxWater(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRJAccountFlow(String date) {
        try {
            frMapper.deleteRJAccountFlow(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRJCzFrWater(String date) {
        try {
            frMapper.deleteRJCzFrWater(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRJCzFxWater(String date) {
        try {
            frMapper.deleteRJCzFxWater(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRJFyWater(String date) {
        try {
            frMapper.deleteRJFyWater(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRJGcWater(String date) {
        try {
            frMapper.deleteRJGcWater(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteMJDetailWater(String date) {
        try {
            frMapper.deleteMJDetailWater(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteTbAccountInfo(String date) {
        try {
            frMapper.deleteTbAccountInfo(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteDLRTbAccountInfo(String date) {
        try {
            frMapper.deleteDLRTbAccountInfo(date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
